package cn.shenlong.web;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class FrontInterceptor extends HandlerInterceptorAdapter {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	public static final String publickey = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKsZnA0ZPlzaUveP8aUYqy3jQkuF3cVsxlOkrbYlS22NPVwL1PHkrMYVlkrt04bxLhEXgHLDe+0kxzkRSEWdnRsCAwEAAQ==";

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {

		if (isExcludeMap(request, response)) {
			return preHandleExcludeMap(request, response, handler);
		}
		logger.info("");
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
	}

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {

	}

	public static <T> T mapConvertBean(Class<T> type, Map<String, Object> map) {
		T t = null;
		try {
			BeanInfo beanInfo = Introspector.getBeanInfo(type);
			// 获取类属性 　
			t = type.newInstance();
			for (PropertyDescriptor descriptor : beanInfo
					.getPropertyDescriptors()) {
				String propertyName = descriptor.getName();
				if (map.containsKey(propertyName)) {
					descriptor.getWriteMethod()
							.invoke(t, map.get(propertyName));
				}
			}
		} catch (IntrospectionException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		}
		return t;
	}

	/**
	 * 获取用户访问的Uri，登入后，返回的调转地址
	 * 
	 * @param request
	 * @return
	 */
	public static String getUri(HttpServletRequest request) {
		// 获取参数
		@SuppressWarnings("unchecked")
		Map<String, Object> paraMap = request.getParameterMap();
		String uri = "";
		if (!paraMap.isEmpty()) {
			Set<Entry<String, Object>> set = paraMap.entrySet();
			@SuppressWarnings("rawtypes")
			Iterator it = set.iterator();
			StringBuffer buffer = new StringBuffer();
			buffer.append("?");
			while (it.hasNext()) {
				@SuppressWarnings("unchecked")
				Map.Entry<String, Object> entry = (Entry<String, Object>) it
						.next();
				buffer.append(entry.getKey() + "=");
				buffer.append(request.getParameter(entry.getKey()) + "&");
			}
			buffer.deleteCharAt(buffer.lastIndexOf("&"));
			uri = buffer.toString();
		}
		return uri;
	}

	/**
	 * 不拦截的路径
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	private boolean isExcludeMap(HttpServletRequest request,
			HttpServletResponse response) {

		if (request.getRequestURL() != null
				&& request.getRequestURL().indexOf("excludeMap") > 0) {
			return true;
		} else {
			return false;
		}
	}

	public boolean preHandleExcludeMap(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		return true;
	}

}
